#include "readF107.h"

#include <iostream>
#include <fstream>
#include <cstdio>
#include ".\..\orbit\DateTime.h"

///<description>calculate the line number of given date in f107 file
///then calculate the line number for previous day and 81 day range
///then find the f107 values
///it's not that everyday has 3 lines
///-----------------------------------------------------------------
///find the 81 day's period start and end
///<param>f107[0] - value of last day, f107[1] - 81 days average</param>
void getF107Data(DateTime givenDT, double f107[2])
{
	//observed data start date
	DateTime obsDateSt(startYear, startMonth, startDay);
	if (givenDT.Compare(obsDateSt) < 0)
	{
		cout << "no data for" << givenDT.ToString() << endl;
		exit(1);
	}
	else
	{
		//find the line number of given date in f107
		//comment: the file "fluxtable.txt"
		//20041028 - 2 lines, -1
		//20041029 - 2 lines, -1
		//20041030 - 3 lines
		//20041031 - 3 lines
		//20041101 - 2 lines, -1
		//>20041102 - 3 lines

		////following value are all start-line numbers
		//int diffDay = (t - it).Days();
		//int lineNumNow = (diffDay - 1) * 3 + 2;
		//int lineNumYesterday = lineNumNow - 3;
		//int line81DaySt = lineNumNow - 120;
		//int line81DayEnd = line81DaySt + 81 * 3;
		//if (line81DaySt < 0)
		//{
		//	cout << "the current fluxtable.txt file doesnt contain data of f107 81 day's average on date" << t.ToString() << endl;
		//	exit(1);
		//}
		//if (lineNumYesterday < 0)
		//{
		//	cout << "the current fluxtable.txt file doesnt contain data of f107 last day's average on date" << t.ToString() << endl;
		//	exit(1);
		//}
		//ifstream f107File;
		//string line;
		//char tmp[f107LineLen];
		//int counter = 0;
		//f107[0] = 0.0; f107[1] = 0.0;
		//f107File.open("fluxtable.txt");
		//streamoff offset = line81DaySt * f107LineLen;
		//f107File.seekg(offset, ios::beg);
		//for(int i=line81DaySt;i<line81DayEnd;i++)
		//{
		//	f107File.getline(tmp, f107LineLen);
		//	line = tmp;
		//		f107[1] += stod(line.substr(54, 8));
		//	if (counter >= lineNumYesterday && counter <= lineNumNow)
		//	{
		//		f107[0] += stod(line.substr(54, 8));
		//	}
		//}
		//f107[0] = f107[0] / 3.0;
		//f107[1] = f107[1] / 243.0;
		//f107File.close();
		DateTime day81st, day81end;
		day81st = givenDT.AddDays(-40.0);
		day81end = givenDT.AddDays(40.0);
		if (day81st.Compare(obsDateSt) < 0)
		{
			cout << "no enough 81 days data for" << givenDT.ToString() << endl;
			exit(1);
		}
		ifstream f107File;
		char tmpline[f107LineLen];
		string line;
		DateTime currentDT;

		f107File.open("fluxtable.txt");
		//skip two head lines
		f107File.getline(tmpline, f107LineLen);
		f107File.getline(tmpline, f107LineLen);

		//start to find the date necessary
		do
		{
			currentDT.Initialise(100, 1, 1, 0, 0, 0, 0);
			f107File.getline(tmpline, f107LineLen);
			line = tmpline;
			currentDT = currentDT.AddYears(stoi( line.substr(0, 4))-100);
			currentDT = currentDT.AddMonths( stoi(line.substr(4, 2))-1);
			currentDT = currentDT.AddDays( stoi(line.substr(6, 2))-1);
		} while (currentDT.Compare(day81st)<0);

		int lineCounter[2] = { 0 ,0};
		f107[0] = 0.0; f107[1] = 0.0;
		
		while (currentDT.Compare(day81end) <= 0)
		{
			//day81's first line alread got
			line = tmpline;
			double value = stod(line.substr(54, 8));
			f107[1] += value;
			lineCounter[1]++;
			if (currentDT.Year() == givenDT.Year() && currentDT.Month() == givenDT.Month() && currentDT.Day() == givenDT.Day())
			{
				f107[0] += value;
				lineCounter[0]++;
			}
			f107File.getline(tmpline, f107LineLen);
			currentDT.Initialise(100, 1, 1, 0, 0, 0, 0);
			currentDT = currentDT.AddYears(stoi(line.substr(0, 4))-100);
			currentDT = currentDT.AddMonths(stoi(line.substr(4, 2))-1);
			currentDT = currentDT.AddDays(stoi(line.substr(6, 2))-1);
		}
		f107[1] = f107[1] / double(lineCounter[1]);
		f107[0] = f107[0] / double(lineCounter[0]);
	}
}